"""
性能测试
测试程序性能
"""

import time
import random
from wc_advanced import (
    count_characters, count_words, count_sentences,
    count_code_lines, count_empty_lines, count_comment_lines
)

def generate_test_text(size_kb=10):
    """生成测试文本"""
    words = ['hello', 'world', 'test', 'python', 'program', 'code', 
             'function', 'class', 'method', 'variable', 'return', 'import']
    
    sentences = []
    target_size = size_kb * 1024  # 转换为字节
    
    while len(' '.join(sentences)) < target_size:
        sentence_length = random.randint(5, 20)
        sentence_words = random.choices(words, k=sentence_length)
        sentence = ' '.join(sentence_words) + random.choice(['.', '!', '?'])
        sentences.append(sentence.capitalize())
    
    return ' '.join(sentences)

def generate_test_code(size_lines=100):
    """生成测试代码"""
    code_lines = []
    for i in range(size_lines):
        if random.random() < 0.1:  # 10% 空行
            code_lines.append("")
        elif random.random() < 0.2:  # 20% 注释行
            code_lines.append(f"# Comment line {i}")
        else:  # 70% 代码行
            code_lines.append(f"def function_{i}():")
    
    return code_lines

def performance_test():
    """性能测试"""
    print("=== 性能测试 ===")
    
    # 测试不同大小的文本
    sizes = [10, 50, 100]  # KB
    
    for size in sizes:
        print(f"\n测试 {size}KB 文本:")
        
        # 生成测试文本
        text = generate_test_text(size)
        lines = text.split('\n')
        
        # 测试字符统计
        start_time = time.time()
        char_count = count_characters(text)
        char_time = time.time() - start_time
        
        # 测试单词统计
        start_time = time.time()
        word_count = count_words(text)
        word_time = time.time() - start_time
        
        # 测试句子统计
        start_time = time.time()
        sentence_count = count_sentences(text)
        sentence_time = time.time() - start_time
        
        print(f"字符统计: {char_count} 字符, 耗时: {char_time:.4f} 秒")
        print(f"单词统计: {word_count} 单词, 耗时: {word_time:.4f} 秒")
        print(f"句子统计: {sentence_count} 句子, 耗时: {sentence_time:.4f} 秒")
    
    # 测试代码分析性能
    print(f"\n测试代码分析性能:")
    code_lines = generate_test_code(1000)
    
    start_time = time.time()
    code_line_count = count_code_lines(code_lines)
    code_time = time.time() - start_time
    
    start_time = time.time()
    empty_line_count = count_empty_lines(code_lines)
    empty_time = time.time() - start_time
    
    start_time = time.time()
    comment_line_count = count_comment_lines(code_lines)
    comment_time = time.time() - start_time
    
    print(f"代码行分析: {code_line_count} 行, 耗时: {code_time:.4f} 秒")
    print(f"空行分析: {empty_line_count} 行, 耗时: {empty_time:.4f} 秒")
    print(f"注释行分析: {comment_line_count} 行, 耗时: {comment_time:.4f} 秒")

def stress_test():
    """压力测试"""
    print("\n=== 压力测试 ===")
    
    # 生成大文本
    large_text = generate_test_text(500)  # 500KB
    print(f"生成了 {len(large_text)/1024:.1f}KB 的测试文本")
    
    start_time = time.time()
    
    # 执行所有统计功能
    char_count = count_characters(large_text)
    word_count = count_words(large_text)
    sentence_count = count_sentences(large_text)
    
    lines = large_text.split('\n')
    code_line_count = count_code_lines(lines)
    empty_line_count = count_empty_lines(lines)
    comment_line_count = count_comment_lines(lines)
    
    total_time = time.time() - start_time
    
    print(f"压力测试结果:")
    print(f"字符数: {char_count}")
    print(f"单词数: {word_count}")
    print(f"句子数: {sentence_count}")
    print(f"代码行数: {code_line_count}")
    print(f"空行数: {empty_line_count}")
    print(f"注释行数: {comment_line_count}")
    print(f"总耗时: {total_time:.4f} 秒")

if __name__ == "__main__":
    performance_test()
    stress_test()